package com.imti.mall.sale.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.feign.clients.PmsProductCategoryFeignClient;
import com.imti.mall.model.cms.pojo.CmsSubject;
import com.imti.mall.model.sms.pojo.SmsHomeNewProduct;
import com.imti.mall.model.sms.pojo.SmsHomeRecommendSubject;
import com.imti.mall.model.sms.vo.SmsHomeNewProductVo;
import com.imti.mall.model.sms.vo.SmsHomeRecommendSubjectVo;
import com.imti.mall.sale.service.SmsHomeRecommendSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.PublicKey;
import java.util.List;

@RestController
@Api(tags = "首页专题推荐表")
@RequestMapping("/api/v1/sale")
public class SmsHomeRecommendSubjectController {
    @Autowired
    SmsHomeRecommendSubjectService smsHomeRecommendSubjectService;


    @Resource
    PmsProductCategoryFeignClient pmsProductCategoryFeignClient;

    @PostMapping("/findSmsHomeRecommendSubject")
    @ApiOperation("查询首页专题推荐表")
    public CommonResult findSmsHomeRecommendSubject(SmsHomeRecommendSubject smsHomeRecommendSubject ,
         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<SmsHomeRecommendSubject> list = smsHomeRecommendSubjectService.findSmsHomeRecommendSubject(smsHomeRecommendSubject,pageSize,pageNum);
       if(list.size()>0){
           return CommonResult.success(CommonPage.restPage(list));
       }else {
           return CommonResult.success("未查询到信息");
       }

    }

    /**
     * 添加首页专题推荐表
     */
    @ApiOperation(value = "添加首页专题推荐表")
    @PostMapping(value = "/addSmsHomeRecommendSubject" )
    public CommonResult addSmsHomeRecommendSubject(@RequestBody List<SmsHomeRecommendSubject> smsHomeRecommendSubject) {
        Integer res=0;
        for(int i=0;i<smsHomeRecommendSubject.size();i++){
            res= smsHomeRecommendSubjectService.addSmsHomeRecommendSubject(smsHomeRecommendSubject.get(i));
        }
        if(res>0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 修改首页专题推荐表
     */
    @ApiOperation(value = "修改首页专题推荐表")
    @PostMapping(value = "/updateSmsHomeRecommendSubject" )
    public CommonResult updateSmsHomeRecommendSubject(SmsHomeRecommendSubjectVo smsHomeRecommendSubjectVo) {
        int res = 0;
        if (smsHomeRecommendSubjectVo.getId() != null) {
            SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectVo;
            res = smsHomeRecommendSubjectService.updateSmsHomeRecommendSubject(smsHomeRecommendSubject);
        } else {
            Long[] ids = smsHomeRecommendSubjectVo.getIds();
            for (int i = 0; i < ids.length; i++) {
                SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectVo;
                smsHomeRecommendSubject.setId(ids[i]);
                res = smsHomeRecommendSubjectService.updateSmsHomeRecommendSubject(smsHomeRecommendSubject);
            }
        }
        if (res > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }


    /**
     * 删除首页专题推荐表
     */
    @ApiOperation(value = "删除首页专题推荐表")
    @PostMapping(value = "/delSmsHomeRecommendSubject" )
    public CommonResult delSmsHomeRecommendSubject( SmsHomeRecommendSubjectVo smsHomeRecommendSubjectVo) {
        Long[] ids= smsHomeRecommendSubjectVo.getIds();
        int res =0;
        for(int i=0;i<ids.length;i++){
            res = smsHomeRecommendSubjectService.delSmsHomeRecommendSubject(ids[i].intValue());
        }
        if(res>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }


   @PostMapping("/findSmsHomeRecommendSubjectApp")
    @ApiOperation("查询首页专题推荐表APP")
    public CommonResult findSmsHomeRecommendSubjectApp(SmsHomeRecommendSubject smsHomeRecommendSubject ,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<SmsHomeRecommendSubject> list = smsHomeRecommendSubjectService.findSmsHomeRecommendSubject(smsHomeRecommendSubject,pageSize,pageNum);
        for (int i=0;i<list.size();i++){
            CmsSubject cmsSubject=pmsProductCategoryFeignClient.getCmsSubject(list.get(i).getSubjectId());
            list.get(i).setCmsSubject(cmsSubject);
        }

            return CommonResult.success(CommonPage.restPage(list));


    }

}
